#pragma once
#include "Common.h"
#include "RadixTree.hpp"

class PageCache
{
public:
    static PageCache* GetInstance();
    std::mutex& GetMtx();
    Span* NewSpan(size_t k);
    Span* MapObjectToSpan(void* obj);
    void ReleaseSpanToPageCache(Span* span);

private:
    PageCache() = default;
    PageCache(const PageCache&) = delete;
    PageCache& operator=(const PageCache&) = delete;

    static PageCache _pageCache;
    SpanList _spanLists[page_num];
    //std::unordered_map<PageID, Span*> _idSpanMap; // g根据pageid查找对应的span
    // 使用基数树替代哈希表
#if defined(_WIN64) || defined(__x86_64__)
    RadixTree3<64 - page_shift> _idSpanMap;
#else
    RadixTree2<32 - page_shift> _idSpanMap;
#endif
    std::set<size_t> _nonEmptyK; // 记录pc中是否存在k页span，不用遍历spanlists
    std::mutex _mtx; // 整个pagecache的锁
};